package cn.doit.delrediskey

import java.util

import cn.doit.utils.JedisUtils
import org.slf4j.LoggerFactory

object RealFlowDelRedisKey {

  val logger = LoggerFactory.getLogger(this.getClass.getSimpleName)

  def main(args: Array[String]): Unit = {
    if(args.length != 2){
      logger.error(s"${this.getClass.getName} agrs is error , day hour")
      sys.exit(1)
    }
    delRedisKey(args(0), args(1))
  }


  def delRedisKey(day: String, hour: String): Unit = {
    val jedis = JedisUtils.getJedis()
    //FLOWREAL@PV@11@20181117@11@25
    //通过模糊查找,找到所有要删除的keys
    try{
      val keys: util.Set[String] = jedis.keys(s"FLOWREAL@*@*@${day}@${hour}@*")

      import scala.collection.JavaConversions._

      for (s <- keys) {
        //遍历删除
        jedis.del(s)
      }
    }catch {
      case e:Exception =>{
        logger.error(s"${this.getClass.getName} delRedisKey :close jedis exception")
        e.printStackTrace()
      }
    }finally {
      if(jedis != null)  jedis.close()
    }



  }
}
